<?php
/* SVN FILE: $Id: item_nameeffect.php 247 2009-10-22 15:31:39Z supersmashbrothers $ */
/**
* ibMarket 2
* IP.Board 3.0
* Name Effect item
*
* @author $Author: supersmashbrothers $
* @copyright $Copyright$
* @version $Revision: 247 $
* @lastrevision $Date: 2009-10-22 15:31:39 +0000 (Thu, 22 Oct 2009) $
* @modifiedby $LastChangedBy: supersmashbrothers $
* @lastmodified $LastChangedDate: 2009-10-22 15:31:39 +0000 (Thu, 22 Oct 2009) $
* @filesource $URL: http://ipb-ibmarket.googlecode.com/svn/ibmarket2/trunk/admin/applications_addon/other/ibmarket/items/own/item_nameeffect.php $
*/

class item_nameeffect extends templateItem
{
	/**
	 * The default name of the item
	 *
	 * @access	protected
	 * @var		string
	 */
	protected $defaultName = "Name Effect";
	
	/**
	 * The default description of the item
	 *
	 * @access	protected
	 * @var		string
	 */
	protected $defaultDescription = "Add an effect to your name.";
	
	// Inherited Properties
	/**
	 * Item setting to allow multiple instances within ibMarket 
	 *
	 * @access	protected
	 * @var		boolean
	 */
	protected $allowMultiple = true;
	
	/**
	 * Item specific setting
	 *
	 * @access	protected
	 * @var		array
	 */
	protected $itemSettings = array();
	
	/**
	 * Constructor to setup the item
	 *
	 * @access	public
	 * @param	ipsRegistry	$registry	IPB registry
	 * @param	array		&$item		The item information
	 */
	public function __construct(ipsRegistry $registry, array &$item)
	{
		parent::__construct($registry, $item);
		
		if($item['item_config'] || $item['item_id'] != 0)
		{
			$this->itemSettings = unserialize($item['item_config']);
		}
		else
		{
			$this->itemSettings['effect'] = 'remove';
		}
	}
	
	/**
	 * Additional settings when adding/editing the item
	 *
	 * @access	public
	 * @return	string|bool	string if there is something to edit, false otherwise
	 */
	public function adminForm()
	{
		$effects = array();
		$effects[] = array('bold',			$this->lang->words['effect_bold']);
		$effects[] = array('italics',		$this->lang->words['effect_italics']);
		$effects[] = array('color',			$this->lang->words['effect_color']);
		$effects[] = array('background',	$this->lang->words['effect_bgcolor']);
		$effects[] = array('dropshadow',	$this->lang->words['effect_dshadow']);
		$effects[] = array('shadow',		$this->lang->words['effect_shadow']);
		$effects[] = array('glow',			$this->lang->words['effect_glow']);
		$effects[] = array('blur',			$this->lang->words['effect_blur']);
		$effects[] = array('flipv',			$this->lang->words['effect_vflip']);
		$effects[] = array('fliph',			$this->lang->words['effect_hflip']);
		$effects[] = array('wave',			$this->lang->words['effect_wave']);
		$effects[] = array('xray',			$this->lang->words['effect_xray']);
		$effects[] = array('invert',		$this->lang->words['effect_invert']);
		$effects[] = array('remove',		$this->lang->words['remove_effect']);
		
		$form['effect'] = ipsRegistry::getClass('output')->formDropdown('item[effect]', $effects, $this->itemSettings['effect']);
		return <<<EOF
				<li>
					<span style='font-weight:bold'>{$this->lang->words['effect_bold']}</span>
					<br />
					<span style='font-style:italic'>{$this->lang->words['effect_italics']}</span>
					<br />
					<span style='color:#FF00FF'>{$this->lang->words['effect_color']}</span>
					<br />
					<span style='background-color:#00FFFF'>{$this->lang->words['effect_bgcolor']}</span>
					<br />
					<span style='display:inline-block; filter: dropshadow(color=#0000FF, offx=5, offy=-3, Positive=1)'>{$this->lang->words['effect_dshadow']}</span>
					<br />
					<span style='display:inline-block; filter: shadow(color=#FF0000, direction=45)'>{$this->lang->words['effect_shadow']}</span>
					<br />
					<span style='display:inline-block; filter: glow(color=#00FF00, strength=6)'>{$this->lang->words['effect_glow']}</span>
					<br />
					<span style='display:inline-block; filter: blur(add=false, direction=140, strength=6)'>{$this->lang->words['effect_blur']}</span>
					<br />
					<span style='display:inline-block; filter: flipv'>{$this->lang->words['effect_vflip']}</span>
					<br />
					<span style='display:inline-block; filter: fliph'>{$this->lang->words['effect_hflip']}</span>
					<br />
					<span style='display:inline-block; filter: wave(strength=2)'>{$this->lang->words['effect_wave']}</span>
					<br />
					<span style='display:inline-block; filter: xray'>{$this->lang->words['effect_xray']}</span>
					<br />
					<span style='display:inline-block; filter: invert'>{$this->lang->words['effect_invert']}</span>
				</li>
				<li>
					<label>
						{$this->lang->words['which_effect']}
					</label>
					{$form['effect']}
				</li>

EOF;
	}
	
	/**
	 * Saving to adding/editing the item
	 *
	 * @access	public
	 * @param	array	&$item_info		The new item information to be stored in the database.
	 * @param	array	[$input]		Relavent input relating to the item
	 * @todo
	 * @return	void
	 */
	public function adminSave(array &$item_info, array $input)
	{
		$setting = array();
		$setting['effect'] = $input['effect'];
		
		$item_info['item_config'] = serialize($setting);
		
		if($this->DB->checkForField("effect_prefix", "profile_portal"))
		{
			$this->DB->changeField("profile_portal","effect_prefix","effect_prefix","varchar(255) NOT NULL","''");
		}
		else
		{
			$this->DB->addField("profile_portal","effect_prefix","varchar(255) NOT NULL","''");
		}
		if($this->DB->checkForField("effect_suffix", "profile_portal"))
		{
			$this->DB->changeField("profile_portal","effect_suffix","effect_suffix","varchar(255) NOT NULL","''");
		}
		else
		{
			$this->DB->addField("profile_portal","effect_suffix","varchar(255) NOT NULL","''");
		}
	}
	
	/**
	 * When quick saving the price and stock
	 *
	 * @access	public
	 * @param	array	&$item_info		The new item information to be stored in the database.
	 * @param	string	$type			Possible values: 'stockprice', more possibly in the future
	 * @return	void
	 */
	public function adminQuickSave(array &$item_info, $type)
	{
		return;
	}
	
	/**
	 * When removing the item from ibMarket
	 *
	 * @access	public
	 * @return	void
	 */
	public function adminRemove()
	{
		if(count(marketRegistry::getItemsByFiles($this->item['filename'])) == 1)
		{
			$this->DB->dropField("profile_portal","effect_prefix");
			$this->DB->dropField("profile_portal","effect_suffix");
		}
	}
	
	/**
	 * Viewing the file edits if any
	 *
	 * @access	public
	 * @return	string|bool	string if there is something to edit, false otherwise
	 */
	public function adminViewFileEdits()
	{
		$adminDir = CP_DIRECTORY;
		$edits = array();
		$edits['topics.php']['line'] = sprintf($this->lang->words['about_the_line_x'],2189);
		$edits['threaded.php']['line'] = sprintf($this->lang->words['about_the_line_x'],260);
		
		return <<<EOF
<div id='skinEditsGlobal'>
	<div class='acp-box'>
		<h3>
			{$this->lang->words['skin_template']}: {$this->lang->words['bit_skin_topic']}
		</h3>
		<table width='100%' cellpadding='0' cellspacing='0' border='0' class='alternate_rows'>
			<tr>
				<th colspan='2'>topicViewTemplate</th>
			</tr>
			<tr>
				<td style='white-space:nowrap;width:1px;min-width:1%'>
					{$this->lang->words['find']}
				</td>
				<td>
					<textarea style="width: 98%;" cols="80" rows="1">{\$post['author']['members_display_name']}</textarea>
				</td>
			</tr>
			<tr>
				<td style='white-space:nowrap;width:1px;min-width:1%'>
					{$this->lang->words['add_before']}
				</td>
				<td>
					<textarea style="width: 98%;" cols="80" rows="10">{\$post['author']['effect_prefix']}</textarea>
				</td>
			</tr>
			<tr>
				<td style='white-space:nowrap;width:1px;min-width:1%'>
					{$this->lang->words['add_after']}
				</td>
				<td>
					<textarea style="width: 98%;" cols="80" rows="10">{\$post['author']['effect_suffix']}</textarea>
				</td>
			</tr>
			<tr>
				<td style='white-space:nowrap;width:1px;min-width:1%'>
					{$this->lang->words['it_will_look_like']}
				</td>
				<td>
					<textarea style="width: 98%;" cols="80" rows="10">{\$post['author']['effect_prefix']}{\$post['author']['members_display_name']}{\$post['author']['effect_suffix']}</textarea>
				</td>
			</tr>
		</table>
	</div>
</div>
EOF;
	}
	
	/**
	 * Any admin AJAX functionality
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	void
	 */
	public function adminAjax()
	{
		return;
	}
	
	/**
	 * Check and modify the intentory item information before storing in the database
	 *
	 * @access	public
	 * @param	array	&$invItem		The new inventory item information to be stored in the database.
	 * @return	bool	true if the item is successfully created, false otherwise
	 */
	public function publicOnCreateItem(array &$invItem)
	{
		return true;
	}
	
	/**
	 * When using the item
	 * Show a form and validate the form
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	string|bool				string if there is a form to be used, true if successfully used, false otherwise
	 */
	public function publicUseItem(array &$invItem)
	{
		$this->registry->getClass('class_localization')->loadLanguageFile( array( 'public_items' ) );
		
		if($this->request['request_method'] == 'post')
		{
			$prefix = "";
			if($this->itemSettings['effect'] == 'bold')
			{
				$prefix = "<span style=\"font-weight:bold\">";
			}
			elseif($this->itemSettings['effect'] == 'italics')
			{
				$prefix = "<span style=\"font-style:italic\">";
			}
			elseif($this->itemSettings['effect'] == 'color')
			{
				$prefix = "<span style=\"color:{$this->request['color']}\">";
			}
			elseif($this->itemSettings['effect'] == 'background')
			{
				$prefix = "<span style=\"background-color:{$this->request['color']}\">";
			}
			elseif($this->itemSettings['effect'] == 'dropshadow')
			{
				$prefix = "<span style=\"display:inline-block; filter:dropshadow(color={$this->request['color']}, offx=5, offy=-3, Positive=true)\">";
			}
			elseif($this->itemSettings['effect'] == 'shadow')
			{
				$prefix = "<span style=\"display:inline-block; filter:shadow(color={$this->request['color']}, direction=45)\">";
			}
			elseif($this->itemSettings['effect'] == 'glow')
			{
				$prefix = "<span style=\"display:inline-block; filter:glow(color={$this->request['color']}, strength=6)\">";
			}
			elseif($this->itemSettings['effect'] == 'blur')
			{
				$prefix = "<span style=\"display:inline-block; filter:blur(add=false, direction=140, strength=6)\">";
			}
			elseif($this->itemSettings['effect'] == 'flipv')
			{
				$prefix = "<span style=\"display:inline-block; filter:flipv\">";
			}
			elseif($this->itemSettings['effect'] == 'fliph')
			{
				$prefix = "<span style=\"display:inline-block; filter:fliph\">";
			}
			elseif($this->itemSettings['effect'] == 'wave')
			{
				$prefix = "<span style=\"display:inline-block; filter:wave(strength=2)\">";
			}
			elseif($this->itemSettings['effect'] == 'xray')
			{
				$prefix = "<span style=\"display:inline-block; filter:xray\">";
			}
			elseif($this->itemSettings['effect'] == 'invert')
			{
				$prefix = "<span style=\"display:inline-block; filter:invert\">";
			}
			$suffix = $prefix ? "</span>" : "";
			
			IPSMember::save( $this->memberData['member_id'], array( 'extendedProfile' => array( 'effect_prefix' => $prefix , 'effect_suffix' => $suffix ) ) );
			
			$this->deleteItems($invItem);
			
			$this->writeLog($invItem, "Applied name effect: ".$prefix."<strong>".$this->memberData['members_display_name']."</strong>".$suffix);
			
			$this->registry->getClass('output')->redirectScreen( $this->lang->words['name_effect_applied'], $this->settings['base_url_with_app'] . "module=inventory" );
		}
		
		require_once( IPSLib::getAppDir('ibmarket')."/sources/multipleEffects.php");
		
		$color = "#FFFFFF";
		if($this->itemSettings['effect'] == 'bold')
		{
			$prefix = "<span id='currentEffect' style=\"font-weight:bold\">";
		}
		elseif($this->itemSettings['effect'] == 'italics')
		{
			$prefix = "<span id='currentEffect' style=\"font-style:italic\">";
		}
		elseif($this->itemSettings['effect'] == 'color')
		{
			$prefix = "<span id='currentEffect' style=\"color:{$color}\">";
		}
		elseif($this->itemSettings['effect'] == 'background')
		{
			$prefix = "<span id='currentEffect' style=\"background-color:{$color}\">";
		}
		elseif($this->itemSettings['effect'] == 'dropshadow')
		{
			$prefix = "<span id='currentEffect' style=\"display:inline-block; filter:dropshadow(color={$color}, offx=5, offy=-3, Positive=true)\">";
		}
		elseif($this->itemSettings['effect'] == 'shadow')
		{
			$prefix = "<span id='currentEffect' style=\"display:inline-block; filter:shadow(color={$color}, direction=45)\">";
		}
		elseif($this->itemSettings['effect'] == 'glow')
		{
			$prefix = "<span id='currentEffect' style=\"display:inline-block; filter:glow(color={$color}, strength=6)\">";
		}
		elseif($this->itemSettings['effect'] == 'blur')
		{
			$prefix = "<span id='currentEffect' style=\"display:inline-block; filter:blur(add=false, direction=140, strength=6)\">";
		}
		elseif($this->itemSettings['effect'] == 'flipv')
		{
			$prefix = "<span id='currentEffect' style=\"display:inline-block; filter:flipv\">";
		}
		elseif($this->itemSettings['effect'] == 'fliph')
		{
			$prefix = "<span id='currentEffect' style=\"display:inline-block; filter:fliph\">";
		}
		elseif($this->itemSettings['effect'] == 'wave')
		{
			$prefix = "<span id='currentEffect' style=\"display:inline-block; filter:wave(strength=2)\">";
		}
		elseif($this->itemSettings['effect'] == 'xray')
		{
			$prefix = "<span id='currentEffect' style=\"display:inline-block; filter:xray\">";
		}
		elseif($this->itemSettings['effect'] == 'invert')
		{
			$prefix = "<span id='currentEffect' style=\"display:inline-block; filter:invert\">";
		}
		$suffix = $prefix ? "</span>" : "";
		
		$html = <<<EOF
<form id='nameEffectForm' method="post" action="{$this->settings['base_url_with_app']}module=inventory&amp;section=item&amp;item={$invItem['i_id']}">
	<div id='nameEffect'class='block_wrap'>
		<h3 id='nameEffectHeader' class='maintitle'>{$this->item['item_name']}</h3>

EOF;
		if(!in_array($this->itemSettings['effect'],array('bold','italics','blur','flipv','fliph','wave','xray','invert')))
		{
			$cmap = array();
			
			for($b = 0; $b < 16; $b++)
			{
				for($g = 0; $g < 16; $g++)
				{
					for($r = 0; $r < 16; $r++)
					{
						$cmap[floor($b/4)] .= "					<area shape='rect' coords='".($g*4+$b%4*64).",".($r*6).",".($g*4+$b%4*64+3).",".($r*6+5)."' class='mapSection' id='".strtoupper(($r == 0 ? "00" : dechex($r*17)).($g == 0 ? "00" : dechex($g*17)).($b == 0 ? "00" : dechex($b*17)))."'>\n";
					}
				}
			}
			$html .= <<<EOF
		<fieldset class="row1">
			<div class='short'>
				<strong>{$this->lang->words['choose_color']}</strong>
			</div>
			<div class='short' id='colorMap'>
				<map name='colormap1' id='colorMap1' class='colorMap'>
{$cmap[0]}
				</map>
				<map name='colormap2' id='colorMap2' class='colorMap'>
{$cmap[1]}
				</map>
				<map name='colormap3' id='colorMap3' class='colorMap'>
{$cmap[2]}
				</map>
				<map name='colormap4' id='colorMap4' class='colorMap'>
{$cmap[3]}
				</map>
				<img usemap='#colormap1' src='{$this->settings['market_images_url']}/color_table-1.png' border='0' /><img usemap='#colormap2' src='{$this->settings['market_images_url']}/color_table-2.png' border='0' />
				<br />
				<img usemap='#colormap3' src='{$this->settings['market_images_url']}/color_table-3.png' border='0' /><img usemap='#colormap4' src='{$this->settings['market_images_url']}/color_table-4.png' border='0' />
				<br />
				<br />
				{$this->lang->words['hex_code']}: <input type='text' id='previewHexColor' value='' class='input_text' /> <input type='button' value='{$this->lang->words['preview']}' class='input_text' id='previewColorButton' />
			</div>
		</fieldset>
<script type='text/javascript'>
$$('.mapSection').each(function(area) {
	area.observe('mouseover',function(event){
		$('previewHexColor').setValue("#"+event.element().identify());
	});
	area.observe('click',function(event){
		$('color').setValue("#"+event.element().identify());
		updateEffect();
	});
});
$('previewColorButton').observe('click',function(){
	$('color').setValue($('previewHexColor').getValue());
	updateEffect();
});
</script>

EOF;
		}
		$html .= <<<EOF
		<fieldset class="row2">
			<ul>

EOF;
		if(!in_array($this->itemSettings['effect'],array('bold','italics','blur','flipv','fliph','wave','xray','invert')))
		{
			$html .= <<<EOF
				<li>
					<label><strong>{$this->lang->words['color_preview']}</strong></label>
					<input type='text' name='color' id='color' value='{$editing[$this->itemSettings['effect']]['color']}' class='input_text' />
				</li>

EOF;
		}
		$html .= <<<EOF
				<li>
					<label><strong>{$this->lang->words['preview']}</strong></label>
					<br />
					<div class='post_block hentry clear'>
						<div class='post_wrap'>
							<h3>
								{$this->registry->getClass('output')->getReplacement("user_online")}
								<span class="author vcard"><a class="url fn" href='{$this->settings['base_url']}showuser={$this->memberData['member_id']}'>{$prefix}{$this->memberData['members_display_name']}{$suffix}</a>{$this->registry->getClass('output')->getTemplate('global')->user_popup($this->memberData['member_id'], $this->memberData['members_seo_name'])}</span>
							</h3>
						</div>
					</div>
				</li>
			</ul>
		</fieldset>
		<fieldset class="submit">
			<input class="input_submit" type="submit" value="{$this->lang->words['apply_name_effect']}" name="submit" />
		</fieldset>
	</div>
</form>
<script type='text/javascript'>
function updateEffect()
{
	switch('{$this->itemSettings['effect']}')
	{
		case 'color':
			$('currentEffect').setStyle({
				color: $('color').getValue()
			});
			break;
		case 'background':
			$('currentEffect').setStyle({
				backgroundColor: $('color').getValue()
			});
			break;
		case 'dropshadow':
			$('currentEffect').setStyle({
				filter: 'dropshadow(color='+$('color').getValue()+', offx=5, offy=-3, Positive=true)'
			});
			break;
		case 'shadow':
			$('currentEffect').setStyle({
				filter: 'shadow(color='+$('color').getValue()+', direction=45)'
			});
			break;
		case 'glow':
			$('currentEffect').setStyle({
				filter: 'glow(color='+$('color').getValue()+', strength=6)'
			});
			break;
	}
}
</script>

EOF;
		return $html;
	}
	
	/**
	 * Any AJAX functionality
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	void
	 */
	public function publicAjax(array &$invItem)
	{
		return;
	}
	
	/**
	 * When the item is being sold back
	 *
	 * @access	public
	 * @param	array	&$invItem	The inventory item information
	 * @return	bool				true if successful, false otherwise
	 */
	public function publicOnResoldItem(array &$invItem)
	{
		return true;
	}
	
	/**
	 * When the item is being restocked
	 *
	 * @access	public
	 * @param	array	&$item	The inventory item information
	 * @return	void
	 */
	public function onItemRestock(array &$item)
	{
		return;
	}
}
